package ext.acae2.util;

import java.io.FileOutputStream;
import java.io.InputStream;

import wt.content.ApplicationData;
import wt.content.ContentServerHelper;
import wt.method.RemoteAccess;
import wt.method.RemoteMethodServer;

public class DownloadUtil implements RemoteAccess {

	public static boolean download(ApplicationData applicationdata, String path)
			throws Exception {
		RemoteMethodServer rms = RemoteMethodServer.getDefault();
		if (!RemoteMethodServer.ServerFlag) {
			return (Boolean) rms.invoke("download", DownloadUtil.class.getName(),
					null, new Class[] { ApplicationData.class, String.class },
					new Object[] { applicationdata, path });

		}
		InputStream inputstream = ContentServerHelper.service
				.findContentStream(applicationdata);
		FileOutputStream fos = new FileOutputStream(path);

		byte[] bs = new byte[1024];
		int len = 0;
		while ((len = inputstream.read(bs)) != -1) {
			fos.write(bs, 0, len);
		}

		return true;
	}
}
